/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package optinsa;

import java.util.ArrayList;
import javax.swing.JFrame;

/**
 *
 * @author falxa
 */
public class Problem 
{
    public static final int WIDTH = 1000;
    public static final int HEIGHT = 300;
    
    private ArrayList<Job> jobs;
    private ArrayList<Ressource> ressources;

    public Problem(ArrayList<Job> jobs, ArrayList<Ressource> ressources) 
    {
        this.jobs = jobs;
        this.ressources = ressources;
    }
    
    public int evaluation()
    {
        int sum, eval = 0;
        for (int i=0; i<this.jobs.size(); i++)
        {
            sum = 0;
            for (int j=0; j<this.jobs.get(i).getTasks().size(); j++)
                sum += this.jobs.get(i).getTasks().get(j).getDuration();
            if (sum > eval) eval = sum;
        }        
        return eval;
    }
    
    public void displayJobs()
    {
        JFrame f = new JFrame();
        f.setTitle("Ordonnancement");
        f.setSize(WIDTH, HEIGHT);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JobGraph jg = new JobGraph(this.jobs);
        f.getContentPane().add(jg);

        f.setVisible(true);
    }

    public void printJobs()
    {
        for (int i=0; i<this.jobs.size(); i++)
            this.jobs.get(i).print();
    }
    
    public ArrayList<Job> getJobs() 
    {
        return jobs;
    }

    public ArrayList<Ressource> getRessources()
    {
        return ressources;
    }

    @Override
    public Problem clone()
    {
        ArrayList<Ressource> ressources = new ArrayList<>();
        ArrayList<Job> jobs = new ArrayList<>();
        
        for (int i=0; i<this.ressources.size(); i++)
        {
           ressources.add(new Ressource(i));
        }
        
        for (int j=0; j<this.jobs.size(); j++)
        {
            jobs.add(new Job(j));
            System.out.println("JOB"+j);
            
            for (int i=0; i<this.jobs.get(j).getTasks().size(); i++)
            {
                Task t = new Task(i, j, ressources.get(this.jobs.get(i).getTasks().get(j).getRessource().getID()), this.jobs.get(i).getTasks().get(j).getDuration());
                System.out.println("TASK"+i+" R"+this.jobs.get(i).getTasks().get(j).getRessource().getID());
                jobs.get(j).addTask(t);
            }
            jobs.get(j).setDate(0);
        }
        
        Problem p = new Problem(jobs, ressources);
        return p;
    }
    
}
